<?php
namespace app\api\controller;
use think\Controller;
use think\Db;
use lib\Curl;
use lib\JSSDK;
class Access extends Base{
    private function accessToken()
    {
        $appid='wx73b2fe7a7e0dab2c';
        $secret='7146595e3109c65bc26933f2e823eac8';
        $header = array("Content-type:application/json;","Accept:application/json","Accept-Version:1.0");
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
        curl_setopt($ch, CURLOPT_POST, false);
        curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
        $data = curl_exec($ch);
        curl_close($ch);
        $data = json_decode($data,true);
        return $data['access_token'];
    }
    public function upAccessToken()
    {
        // $time=time();
        // $res=Db::name('system_set')->where('id',1)->find();
        // if ($time>$res['access_token_time']+280) {
        //     $result=$this->accessToken();
        //     $data['uptime']=$time;
        //     $data['access_token']=$result;
        //     $data['access_token_time']=$time;
        //     Db::name('system_set')->where('id',1)->update($data);
        // }
        // exit();
        $time=time();
        $res=Db::name('system_set')->where('id',1)->find();
        if ($time>$res['access_token_time']+280) {
            $result=$this->accessToken();
            $data['uptime']=$time;
            $data['access_token']=$result;
            $data['access_token_time']=$time;
            Db::name('system_set')->where('id',1)->update($data);
            return $result;
        }else{
            return $res['access_token'];
        }
    }
    public function userOpenid()
    {
        $url='https://api.weixin.qq.com/sns/oauth2/access_token';
        $urlParams['appid']='wx73b2fe7a7e0dab2c';
        $urlParams['secret']='7146595e3109c65bc26933f2e823eac8';
        $urlParams['code']=input("param.code");
        $urlParams['grant_type']='authorization_code';
        $res=Curl::https_curl($url,$urlParams);
        $data = json_decode($res,true);
        return json(['code' => 200, 'msg' => '成功！','data'=>$data]);
    }
    public function userinfo()
    {
        $url='https://api.weixin.qq.com/sns/userinfo';
        $urlParams['access_token']=input("param.access_token");
        $urlParams['openid']=input("param.openid");
        $urlParams['lang']='zh_CN';
        $res=Curl::https_curl($url,$urlParams);
        $data = json_decode($res,true);
        // $exituser=Db::name('users')->where('openid',$data['openid'])->find();
        // if ($exituser) {

        // }else{
        //     $datap['openid']=$data['openid'];
        //     $datap['headimgurl']=$data['headimgurl'];
        //     $datap['nickname']=$data['nickname'];
        //     $datap['time']=time();
        //     $datap['uptime']=time();
        //     Db::name('users')->insert($datap);
        // }
        return json(['code' => 200, 'msg' => '成功！','data'=>$data]);
    }
    public function wxPosition()
    {
        $url='https://apis.map.qq.com/ws/geocoder/v1/';
        $urlParams['coord_type']=5;
        $urlParams['get_poi']='0';
        $urlParams['output']='json';
        $urlParams['key']='GIJBZ-BGCRP-EMFDH-VEOP2-LB23S-YEB6P';
        // $urlParams['location']='30.5445216900,114.2454528800';
        $urlParams['location']=input("param.location");
        $res=Curl::https_curl($url,$urlParams);
        $data = json_decode($res,true);
        return json(['code' => 200, 'msg' => '成功！','data'=>$data]);
    }
    public function onShare()
    {
        $url = urldecode(input("param.url"));
        $appid = 'wx73b2fe7a7e0dab2c';
        $appsecret = '7146595e3109c65bc26933f2e823eac8';
        $jssdk = new JSSDK($appid, $appsecret, $url);
        $signPackage = $jssdk->GetSignPackage();
        $appId = $signPackage['appId'];
        $timestamp = $signPackage['timestamp'];
        $nonceStr = $signPackage['nonceStr'];
        $signature = $signPackage['signature'];
        $surl = $signPackage['url'];
        $data = array("appId"=>$appId,"timestamp"=>$timestamp,"nonceStr"=>$nonceStr,"signature"=>$signature,"surl"=>$surl); //返回给前端的数据
        return json(['code' => 200, 'msg' => '成功！','data'=>$data]);
        
    }
}